home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / EXAMPLES / DIALS2.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  2.8 KB  |  141 lines

  1.  
  2. /* Copyright (c) Mark J. Kilgard, 1994. */
  3.  
  4. /* This program is freely distributable without licensing fees 
  5.    and is provided without guarantee or warrantee expressed or 
  6.    implied. This program is -not- in the public domain. */
  7.  
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <math.h>
  12. #include <GL/glut.h>
  13.  
  14. #define NUM_DIALS 8
  15. #define NUM_BUTTONS 32
  16.  
  17. int *dials, *buttons;
  18.  
  19. #undef PI /* Some systems may have this defined. */
  20. #define PI            3.14159265358979323846
  21.  
  22. void
  23. drawCircle(int x, int y, int r, int dir)
  24. {
  25.    float angle;
  26.  
  27.    glPushMatrix();
  28.    glTranslatef(x,y,0);
  29.    glBegin(GL_TRIANGLE_FAN);
  30.    glVertex2f(0,0);
  31.    for(angle = 2*PI; angle >= 0; angle -= PI/12) {
  32.       glVertex2f(r*cos(angle),r*sin(angle));
  33.    }
  34.    glEnd();
  35.    glColor3f(0,0,1);
  36.    glBegin(GL_LINES);
  37.    glVertex2f(0,0);
  38.    glVertex2f(r*cos(dir*PI/180),r*sin(dir*PI/180));
  39.    glEnd();
  40.    glPopMatrix();
  41. }
  42.  
  43. void
  44. displayDials(void)
  45. {
  46.   int i;
  47.  
  48.   for(i=0;i<NUM_DIALS;i++) {
  49.     glColor3f(0, 1, 0);
  50.     drawCircle(60 + ((i+1)%2) * 100, 60 + (i/2) * 100, 40, dials[NUM_DIALS-1-i]-90);
  51.   }
  52. }
  53.  
  54. void
  55. displayButtons(void)
  56. {
  57.   int i, n;
  58.  
  59.   glBegin(GL_QUADS);
  60.   for(i=0,n=0;i<NUM_BUTTONS;i++,n++) {
  61.     switch(n) {
  62.     case 0:
  63.     case 5:
  64.     case 30:
  65.       n++;
  66.     }
  67.     if(buttons[i]) {
  68.       glColor3f(1,0,0);
  69.     } else {
  70.       glColor3f(1,1,1);
  71.     }
  72.     glVertex2f((n%6)*40+250,(n/6)*40+10);
  73.     glVertex2f((n%6)*40+270,(n/6)*40+10);
  74.     glVertex2f((n%6)*40+270,(n/6)*40+30);
  75.     glVertex2f((n%6)*40+250,(n/6)*40+30);
  76.   }
  77.   glEnd();
  78. }
  79.  
  80. void
  81. display(void)
  82. {
  83.   glClear(GL_COLOR_BUFFER_BIT);
  84.   displayDials();
  85.   displayButtons();
  86.   glutSwapBuffers();
  87. }
  88.  
  89. void
  90. reshape(int w, int h)
  91. {
  92.   glViewport(0, 0, w, h);
  93.   glMatrixMode(GL_PROJECTION);
  94.   glLoadIdentity();
  95.   gluOrtho2D(0, w, 0, h);
  96.   glScalef(1, -1, 1);
  97.   glTranslatef(0, -h, 0);
  98. }
  99.  
  100. void
  101. dodial(int dial, int value)
  102. {
  103.   if(dial > 0 && dial <= NUM_DIALS) {
  104.   dials[dial - 1] = value % 360;
  105.   glutPostRedisplay();
  106.   }
  107. }
  108.  
  109. void
  110. dobutton(int button, int state)
  111. {
  112.   if(button > 0 && button <= NUM_BUTTONS) {
  113.     buttons[button-1] = (state == GLUT_DOWN);
  114.     glutPostRedisplay();
  115.   }
  116. }
  117.  
  118. int
  119. main(int argc, char **argv)
  120. {
  121.   int width, height;
  122.   glutInit(&argc, argv);
  123.   dials = (int*) calloc(NUM_DIALS, sizeof(int));
  124.   buttons = (int*) calloc(NUM_BUTTONS, sizeof(int));
  125.   glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  126.   width = 240 + 240;
  127.   height = 100*((NUM_DIALS+1)/2) + 20;
  128.   if(height < 240) height = 240;
  129.   glutInitWindowSize(width, height);
  130.   glutCreateWindow("GLUT dials & buttons");
  131.   glClearColor(0.5, 0.5, 0.5, 1.0);
  132.   glLineWidth(3.0);
  133.   glutDialsFunc(dodial);
  134.   glutButtonBoxFunc(dobutton);
  135.   glutDisplayFunc(display);
  136.   glutReshapeFunc(reshape);
  137.   glutInitWindowSize(240, 240);
  138.   glutMainLoop();
  139.   return 0;             /* ANSI C requires main to return int. */
  140. }
  141.